/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PantallaPruebas.java
 *
 * Created on 30-abr-2012, 23:56:21
 */

package edu.uoc.tds.pec4.swingx;

import edu.uoc.tds.pec4.common.RemotoInterface;
import edu.uoc.tds.pec4.i18n.GestorIdiomas;
import edu.uoc.tds.pec4.model.Prueba;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.*;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Fernando
 */
public class PantallaPruebasLista extends javax.swing.JDialog {
    private PantallaPruebas pantallaPruebas;
    private RemotoInterface remoto;
    private boolean editable;

    private DefaultTableModel modeloTabla;
    private Object datosTabla[][];
    private Object nombresDeColumnaTabla[];

    private JScrollPane jScrollPanePruebas;
    private JTable jtablePruebas;

    /** Creates new form PantallaPruebas */
    public PantallaPruebasLista(PantallaPruebas pantallaPruebas, boolean modal) {
        this.pantallaPruebas = pantallaPruebas;
        this.remoto = pantallaPruebas.getRemoto();
        initComponents();
        myInitComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        btnExit = new javax.swing.JButton();
        jPanelPruebas = new javax.swing.JPanel();
        jlblPrueba = new javax.swing.JLabel();
        jtxtId = new javax.swing.JTextField();
        jtxtDescripcion = new javax.swing.JTextField();
        jlblDesde = new javax.swing.JLabel();
        jtxtInscripcionDesde = new javax.swing.JTextField();
        jlblHasta = new javax.swing.JLabel();
        jtxtInscripcionHasta = new javax.swing.JTextField();
        jbtnBuscar = new javax.swing.JButton();
        jbtnAceptar = new javax.swing.JButton();
        jlblFechaDescripcion = new javax.swing.JLabel();
        jPanelTabla = new javax.swing.JPanel();
        jmnuBarPruebasLista = new javax.swing.JMenuBar();
        jmnuArchivo = new javax.swing.JMenu();
        jmnuNuevo = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jmnuSalir = new javax.swing.JMenuItem();
        jmnuAyuda = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("eOlympics - Pruebas");

        jToolBar1.setRollover(true);
        jToolBar1.add(jSeparator4);

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Exit.png"))); // NOI18N
        btnExit.setToolTipText("Salir");
        btnExit.setFocusable(false);
        btnExit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnExit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitsalir(evt);
            }
        });
        jToolBar1.add(btnExit);

        jlblPrueba.setText("Prueba");

        jtxtId.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jtxtDescripcion.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jlblDesde.setText("Desde (DD/MM/AAAA)");

        jlblHasta.setText("Hasta (DD/MM/AAAA)");

        jbtnBuscar.setText("Buscar");
        jbtnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnBuscarActionPerformed(evt);
            }
        });

        jbtnAceptar.setText("Aceptar");
        jbtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAceptarActionPerformed(evt);
            }
        });

        jlblFechaDescripcion.setText("Descripción");

        jPanelTabla.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanelTabla.setOpaque(false);

        javax.swing.GroupLayout jPanelTablaLayout = new javax.swing.GroupLayout(jPanelTabla);
        jPanelTabla.setLayout(jPanelTablaLayout);
        jPanelTablaLayout.setHorizontalGroup(
            jPanelTablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 486, Short.MAX_VALUE)
        );
        jPanelTablaLayout.setVerticalGroup(
            jPanelTablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 219, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanelPruebasLayout = new javax.swing.GroupLayout(jPanelPruebas);
        jPanelPruebas.setLayout(jPanelPruebasLayout);
        jPanelPruebasLayout.setHorizontalGroup(
            jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPruebasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanelTabla, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelPruebasLayout.createSequentialGroup()
                        .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlblDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanelPruebasLayout.createSequentialGroup()
                                .addComponent(jlblPrueba)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtxtId, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelPruebasLayout.createSequentialGroup()
                                .addComponent(jlblFechaDescripcion)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtxtDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE))
                            .addGroup(jPanelPruebasLayout.createSequentialGroup()
                                .addComponent(jtxtInscripcionDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jlblHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jtxtInscripcionHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelPruebasLayout.createSequentialGroup()
                        .addComponent(jbtnBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 352, Short.MAX_VALUE)
                        .addComponent(jbtnAceptar)))
                .addContainerGap())
        );
        jPanelPruebasLayout.setVerticalGroup(
            jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPruebasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblPrueba)
                    .addComponent(jtxtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblFechaDescripcion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblDesde)
                    .addComponent(jtxtInscripcionDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtInscripcionHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblHasta))
                .addGap(18, 18, 18)
                .addComponent(jPanelTabla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnBuscar)
                    .addComponent(jbtnAceptar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jmnuArchivo.setText("Archivo");
        jmnuArchivo.setFont(new java.awt.Font("Tahoma", 1, 11));

        jmnuNuevo.setText("Nuevo");
        jmnuArchivo.add(jmnuNuevo);
        jmnuArchivo.add(jSeparator1);

        jmnuSalir.setText("Salir");
        jmnuArchivo.add(jmnuSalir);

        jmnuBarPruebasLista.add(jmnuArchivo);

        jmnuAyuda.setText("Ayuda");
        jmnuAyuda.setFont(new java.awt.Font("Tahoma", 1, 11));
        jmnuBarPruebasLista.add(jmnuAyuda);

        setJMenuBar(jmnuBarPruebasLista);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(456, Short.MAX_VALUE))
            .addComponent(jPanelPruebas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelPruebas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitsalir(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitsalir
        // TODO add your handling code here:
        this.setVisible(false);
}//GEN-LAST:event_btnExitsalir

    private void jbtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarActionPerformed
        // TODO add your handling code here:
        int id_prueba = 0;
        int fila = 0;

        try {
            fila = this.jtablePruebas.getSelectedRow();
            id_prueba = (Integer) this.jtablePruebas.getValueAt(fila,0);

            this.pantallaPruebas.mostrarRegistro(id_prueba);

            this.setVisible(false);
        }
        
        catch(ArrayIndexOutOfBoundsException exc){
            JOptionPane.showMessageDialog(null, "Elija una Prueba a mostrar o cierre la ventana", GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
        }

        catch (RemoteException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
        }

        catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_jbtnAceptarActionPerformed

    private void jbtnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnBuscarActionPerformed
        // TODO add your handling code here:
        this.actualizarDatosTabla();
}//GEN-LAST:event_jbtnBuscarActionPerformed

    private void actualizarDatosTabla(){
        this.limpiarTabla();;
        this.rellenarTablePruebas();
    }

    /**
     * MÃ©todo para crear y definir un objeto tipo jtable, asignando sus propiedades fundamentales  
     * {pre: la clase ha de estar instanciada previamente y el objeto definido}
     * {post: establece los nombres de columna y el modeloTabla predeterminado}
     */
    private void crearTabla(){
        try{
            this.datosTabla = new Object [][]{};
            this.nombresDeColumnaTabla = new String []{"Identificador", "Descripción"};

            this.modeloTabla = new DefaultTableModel(this.datosTabla,this.nombresDeColumnaTabla);

            this.jtablePruebas.setModel(this.modeloTabla);
            this.jScrollPanePruebas.setViewportView(this.jtablePruebas);
            
            javax.swing.GroupLayout jPanelTablaLayout = new javax.swing.GroupLayout(jPanelTabla);
            jPanelTabla.setLayout(jPanelTablaLayout);
            jPanelTablaLayout.setHorizontalGroup(
                jPanelTablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanelTablaLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPanePruebas, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                    .addContainerGap())
            );

            jPanelTablaLayout.setVerticalGroup(
                jPanelTablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelTablaLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPanePruebas, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            );
        }

        /** Capturamos excepciÃ³n de carÃ¡cter general*/
        catch (Exception exc) {
            JOptionPane.showMessageDialog(null, GestorIdiomas.getMessage("error.general"), GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * MÃ©todo para inicializar los componentes contenidos y asociados a un objeto jtable
     * {pre: el objeto jtable ha de estar creado
     * {post: permite que el objeto este disponible para mostrar nuevos datos
     */
    private void limpiarTabla(){
        for(int fila = this.modeloTabla.getRowCount()-1; fila >= 0; fila--){
            this.modeloTabla.removeRow(fila);
        }
    }

    /**
     * MÃ©todo para rellenar los resultados de la participaciÃ³n en un jtable sin diferenciar por universidad
     * {pre: la clase ha de estar instanciada previamente y el objeto definido}
     * {post: establece contenidos, la valoraciÃ³n, de las celdas, informando del nÃºmero de respuestas por cada tipo de pregunta/respuesta}
     */
    public void rellenarTablePruebas(){
        try{
            for(Prueba prueba: this.remoto.getListaPruebas()){
                int id_Prueba = prueba.getId_prueba();
                String descripcion_Prueba = prueba.getDescripcion_prueba();

                this.modeloTabla.addRow(new Object[]{id_Prueba, descripcion_Prueba});
            }
        }

        /** Capturamos excepciÃ³n asociada al servidor RMI*/
        catch (RemoteException exc) {
            JOptionPane.showMessageDialog(null, GestorIdiomas.getMessage("info.servidor.no.iniciado"), GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
        }

        /** Capturamos excepciÃ³n de carÃ¡cter general*/
        catch (Exception exc) {
            JOptionPane.showMessageDialog(null, GestorIdiomas.getMessage("error.general"), GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PantallaPruebas dialog = null;
                try {
                    dialog = new PantallaPruebas(new PantallaMenuPrincipal(), true);
                }

                catch (RemoteException ex) {
                    Logger.getLogger(PantallaPruebasLista.class.getName()).log(Level.SEVERE, null, ex);
                }

                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private void limpiarPantalla(){
        this.jtxtId.setText(null);
        this.jtxtDescripcion.setText(null);
        this.jtxtInscripcionDesde.setText(null);
        this.jtxtInscripcionHasta.setText(null);
    }

    private void myInitComponents(){
        this.jScrollPanePruebas = new JScrollPane();
        this.jtablePruebas = new JTable();

        this.jPanelTabla.add(this.jScrollPanePruebas);

        this.crearTabla();
        this.limpiarTabla();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JPanel jPanelPruebas;
    private javax.swing.JPanel jPanelTabla;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jbtnAceptar;
    private javax.swing.JButton jbtnBuscar;
    private javax.swing.JLabel jlblDesde;
    private javax.swing.JLabel jlblFechaDescripcion;
    private javax.swing.JLabel jlblHasta;
    private javax.swing.JLabel jlblPrueba;
    private javax.swing.JMenu jmnuArchivo;
    private javax.swing.JMenu jmnuAyuda;
    private javax.swing.JMenuBar jmnuBarPruebasLista;
    private javax.swing.JMenuItem jmnuNuevo;
    private javax.swing.JMenuItem jmnuSalir;
    private javax.swing.JTextField jtxtDescripcion;
    private javax.swing.JTextField jtxtId;
    private javax.swing.JTextField jtxtInscripcionDesde;
    private javax.swing.JTextField jtxtInscripcionHasta;
    // End of variables declaration//GEN-END:variables

}
